import { BundleManager } from "../BundleManager";
import GameManager from "../GameManager";
import SharkSayManager, { SharkSayType } from "../SharkSayManager";
import { TweenType } from "../WTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EndTalk extends cc.Component {

    // onLoad () {}

    Init() {
        this.node.opacity = 0;
        this.node.setParent(GameManager.Instance.UINode);
        this.sharkTween(0.5, 5, 0.1);
        cc.tween(this.node)
            .to(1, { opacity: 255 })
            .call(() => {
                this.EndSay(SharkSayManager.GetSharkSayDate(SharkSayType.最终结局), () => {
                    cc.tween(this.node.getChildByName("鲨鱼"))
                        .to(2, { opacity: 0 })
                        .start()
                    cc.tween(this.node.getChildByName("对话框"))
                        .to(2, { opacity: 0 })
                        .start()
                    GameManager.Instance.ShowOverPanel(GameManager.SharkHurted);
                });
            })
            .start()

        let light = this.node.getChildByName("光");
        cc.tween(light)
            .to(3, { angle: 360 })
            .call(() => {
                light.angle = 0;
            })
            .union()
            .repeatForever()
            .start()
        cc.tween(light)
            .to(0.5, { scale: 1.2 })
            .to(0.5, { scale: 1 })
            .union()
            .repeatForever()
            .start()

        cc.tween(light)
            .to(1, { opacity: 200 })
            .to(1, { opacity: 255 })
            .union()
            .repeatForever()
            .start()
    }

    sharkTween(duration: number, angle: number, scaley: number) {
        let shark = this.node.getChildByName("鲨鱼");
        let _scaley = shark.scaleY;
        cc.tween(shark)
            .to(duration, { angle: angle, scaleY: _scaley - scaley })
            .to(duration, { angle: 0, scaleY: _scaley + scaley })
            .to(duration, { angle: -angle, scaleY: _scaley - scaley })
            .to(duration, { angle: 0, scaleY: _scaley + scaley })
            .union()
            .repeatForever()
            .start()
    }

    //鲨鱼讲话
    EndSay(VoiceIndex: number[], callback?: Function) {
        if (VoiceIndex.length == 0) {//完成
            if (callback) callback();
        } else {//没有完成继续读
            let Text: string = GameManager.Instance.SayDate.json[VoiceIndex[0]].SayDate;
            let AnimaName: string = GameManager.Instance.SayDate.json[VoiceIndex[0]].animation;
            BundleManager.GetBundle("AudioClips").load(Text, cc.AudioClip, (err, event) => {
                let AUDIOID;
                if (err) {
                    console.log("没有找到音频文件:" + Text);
                    AUDIOID = cc.audioEngine.playEffect(GameManager.Instance.defaultaudio, false);
                } else {
                    AUDIOID = cc.audioEngine.playEffect(event, false);
                }
                this.node.getChildByName("对话框").getComponentInChildren(cc.Label).string = Text;
                this.SetAnimation(AnimaName);
                VoiceIndex.splice(0, 1);
                cc.audioEngine.setFinishCallback(AUDIOID, () => {
                    this.EndSay(VoiceIndex, callback);
                });
            })
            GameManager.Instance.SharkAction(VoiceIndex[0]);
        }
    }


    //设置鲨鱼动画(表情)
    SetAnimation(AnimationName: string) {
        this.node.getComponentInChildren(cc.Animation).play(AnimationName);
    }



    // update (dt) {}
}
